using ICSharpCode.SharpZipLib.Zip;
using System;
using System.IO;
using System.IO.IsolatedStorage;
namespace Salient.SharpZipLib.Zip
{
	public class IsolatedDiskArchiveStorage : BaseArchiveStorage
	{
		private readonly string fileName_;
		private string temporaryName_;
		private Stream temporaryStream_;
		public IsolatedDiskArchiveStorage(IsolatedZipFile file) : this(file, FileUpdateMode.Safe)
		{
		}
		public IsolatedDiskArchiveStorage(IsolatedZipFile file, FileUpdateMode updateMode) : base(updateMode)
		{
			if (file.Name == null)
			{
				throw new ZipException("Cant handle non file archives");
			}
			this.fileName_ = file.Name;
		}
		public override Stream ConvertTemporaryToFinal()
		{
			if (this.temporaryStream_ == null)
			{
				throw new ZipException("No temporary stream has been created");
			}
			string tempFileName = IsolatedDiskArchiveStorage.GetTempFileName(this.fileName_, false);
			bool flag = false;
			IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
			Stream result;
			try
			{
				this.temporaryStream_.Close();
				userStoreForApplication.MoveFile(this.fileName_, tempFileName);
				userStoreForApplication.MoveFile(this.temporaryName_, this.fileName_);
				flag = true;
				userStoreForApplication.DeleteFile(tempFileName);
				result = userStoreForApplication.OpenFile(this.fileName_, 3, 1, 1);
			}
			catch (Exception)
			{
				if (!flag)
				{
					userStoreForApplication.MoveFile(tempFileName, this.fileName_);
					userStoreForApplication.DeleteFile(this.temporaryName_);
				}
				throw;
			}
			return result;
		}
		public override void Dispose()
		{
			if (this.temporaryStream_ != null)
			{
				this.temporaryStream_.Close();
			}
		}
		public override Stream GetTemporaryOutput()
		{
			if (this.temporaryName_ != null)
			{
				this.temporaryName_ = IsolatedDiskArchiveStorage.GetTempFileName(this.temporaryName_, true);
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					this.temporaryStream_ = userStoreForApplication.OpenFile(this.temporaryName_, 4, 2, 0);
					goto IL_89;
				}
			}
			this.temporaryName_ = Guid.NewGuid().ToString("N");
			using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
			{
				this.temporaryStream_ = userStoreForApplication.OpenFile(this.temporaryName_, 4, 2, 0);
			}
			IL_89:
			return this.temporaryStream_;
		}
		public override Stream MakeTemporaryCopy(Stream stream)
		{
			stream.Close();
			this.temporaryName_ = IsolatedDiskArchiveStorage.GetTempFileName(this.fileName_, true);
			using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
			{
				userStoreForApplication.CopyFile(this.fileName_, this.temporaryName_);
				this.temporaryStream_ = userStoreForApplication.OpenFile(this.temporaryName_, 3, 3);
			}
			return this.temporaryStream_;
		}
		public override Stream OpenForDirectUpdate(Stream stream)
		{
			Stream result;
			if (stream == null || !stream.get_CanWrite())
			{
				if (stream != null)
				{
					stream.Close();
				}
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					result = userStoreForApplication.OpenFile(this.fileName_, 3, 3);
					return result;
				}
			}
			result = stream;
			return result;
		}
		private static string GetTempFileName(string original, bool makeTempFile)
		{
			string text = null;
			if (original == null)
			{
				text = Guid.NewGuid().ToString("N");
			}
			else
			{
				int num = 0;
				int second = DateTime.get_Now().get_Second();
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					while (text == null)
					{
						num++;
						string text2 = string.Format("{0}.{1}{2}.tmp", original, second, num);
						if (!userStoreForApplication.FileExists(text2))
						{
							if (makeTempFile)
							{
								try
								{
									using (userStoreForApplication.CreateFile(text2))
									{
									}
									text = text2;
									continue;
								}
								catch
								{
									second = DateTime.get_Now().get_Second();
									continue;
								}
							}
							text = text2;
						}
					}
				}
			}
			return text;
		}
	}
}
